package com.ccf.business.etl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.utils.constant.Constants;
import com.ccf.business.etl.model.Node;
import com.ccf.business.etl.service.INodeService;
import com.common.model.exception.ServiceException;
import com.common.model.result.CodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.common.model.result.PageResult;
import com.common.model.pojo.Query;
import com.service.mybatis.util.Condition;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.service.mybatis.service.impl.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;

import com.ccf.business.etl.model.RuleGroup;
import com.ccf.business.etl.mapper.RuleGroupMapper;
import com.ccf.business.etl.service.IRuleGroupService;

/**
 * 数据分组管理
 *
 * @author zhaoj
 * @date 2021-11-29 10:41:10
 */
@Slf4j
@Service
public class RuleGroupServiceImpl extends SuperServiceImpl<RuleGroupMapper, RuleGroup> implements IRuleGroupService {

    @Autowired
    private INodeService nodeService;

    /**
    * 列表
    * @param query
    * @return
    */
    @Override
    public PageResult<RuleGroup> findList(Query query){
        Page<RuleGroup> page = page(Condition.getPage(query), Condition.getQueryWrapper(query));
        return PageResult.<RuleGroup>builder().datas(page.getRecords()).code(CodeEnum.SUCCESS.getCode()).count(page.getTotal()).build();
    }

    @Override
    public boolean deleteValidate(String ids) {
        String[] idArrays = StrUtil.split(ids, Constants.COMMA);

        LambdaQueryWrapper<Node> qw = Wrappers.<Node>lambdaQuery();
        for (String id : idArrays) {
            qw.or().like(Node::getParams, id);
        }
        long count = nodeService.count(qw);
        if(count > 0) {
            return false;
        }

        return true;
    }
}
